package com.beancore.util;

import com.beancore.entity.Score;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
    public static String readFileToString(String filePath) throws IOException {
        StringBuilder sb = new StringBuilder();
        File file = new File(filePath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(new String(line.getBytes(), "utf-8")).append("\r\n");
        }
        br.close();
        return sb.toString();
    }

    public static void writeScore(List<Score> scoreList, String filePath) throws FileNotFoundException, IOException {
        ObjectOutputStream objOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
        for (Score score : scoreList) {
            objOutputStream.writeObject(score);
        }
        objOutputStream.writeObject(null);
        objOutputStream.flush();
        objOutputStream.close();
    }

    public static List<Score> readScore(String filePath) throws FileNotFoundException, IOException,
            ClassNotFoundException {
        List<Score> scoreList = new ArrayList<Score>();
        ObjectInputStream objInputStream = new ObjectInputStream(new FileInputStream(filePath));
        Object obj = null;
        while ((obj = objInputStream.readObject()) != null) {
            scoreList.add((Score) obj);
        }
        objInputStream.close();
        return scoreList;
    }
}